package com.hhf.springboot.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Author hhf
 * @Date 2025/6/15
 * @Description 初始化RabbitMq信息, 比如交换机, 队列等
 * <p>
 * 系统运行时只执行一次
 */
public class InitBIMq {

    public static void main(String[] args) {


        try {
            //1. 创建连接工厂
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            //2. 创建连接
            Connection connection = factory.newConnection();
            //3. 创建通道
            Channel channel = connection.createChannel();
            //4. 定义交换机名称
            channel.exchangeDeclare(BiMqConstant.BI_EXCHANGE, "direct");
            //5. 创建队列
            channel.queueDeclare(BiMqConstant.BI_QUEUE, true, false, false, null);
            //6. 将队列绑定到交换机
            channel.queueBind(BiMqConstant.BI_QUEUE, BiMqConstant.BI_EXCHANGE, BiMqConstant.BI_ROUTING_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

}
